
package com.usersmodule.servlet;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UtilDAO;

import com.usersmodule.bean.TbModule;
import com.usersmodule.bean.TbUsers;
import com.usersmodule.dao.TbModuleDAO;
import com.usersmodule.dao.TbUsersDAO;

public class TbUsersServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        String userName=request.getParameter("userName");
        String userNickName=request.getParameter("userNickName");
        String userCardId=request.getParameter("userCardId");
        String userWorkId=request.getParameter("userWorkId");
        String userPass=request.getParameter("userPass");
        String userPhone=request.getParameter("userPhone");
        String userEmail=request.getParameter("userEmail");
        String userAddress=request.getParameter("userAddress");
        String userBirthday=request.getParameter("userBirthday");
        String userInCompanyTime=request.getParameter("userInCompanyTime");
        int userState=Integer.parseInt(request.getParameter("userState"));
        int parentUserId=Integer.parseInt(request.getParameter("parentUserId"));
        int createUserId=((TbUsers)request.getSession().getAttribute("Users")).getUserId();
        int isEnable=Integer.parseInt(request.getParameter("isEnable"));
        int isAdmin=Integer.parseInt(request.getParameter("isAdmin"));
       // int userLoginCount=Integer.parseInt(request.getParameter("userLoginCount"));
        //String UserLastLoginTime=request.getParameter("");
        String userMemo=request.getParameter("userMemo");
            if(new TbUsersDAO().add(new TbUsers(userName,userNickName,userCardId,userWorkId,userPass,userPhone,userEmail,userAddress,userBirthday,userInCompanyTime,userState,parentUserId,createUserId,isEnable,userMemo,isAdmin))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
           goList(request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_Users", "UserId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(request, response);
        }
        if("edit".equals(method)){
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName=request.getParameter("userName");
        String userNickName=request.getParameter("userNickName");
        String userCardId=request.getParameter("userCardId");
        String userWorkId=request.getParameter("userWorkId");
        String userPass=request.getParameter("userPass");
        String userPhone=request.getParameter("userPhone");
        String userEmail=request.getParameter("userEmail");
        String userAddress=request.getParameter("userAddress");
        String userBirthday=request.getParameter("userBirthday");
        String userInCompanyTime=request.getParameter("userInCompanyTime");
        int userState=Integer.parseInt(request.getParameter("userState"));
        int parentUserId=Integer.parseInt(request.getParameter("parentUserId"));
        int createUserId=((TbUsers)request.getSession().getAttribute("Users")).getUserId();
        int isEnable=Integer.parseInt(request.getParameter("isEnable"));
        String userMemo=request.getParameter("userMemo");
        int isAdmin=Integer.parseInt(request.getParameter("isAdmin"));
        //int userLoginCount=Integer.parseInt(request.getParameter("userLoginCount"));
            if(new TbUsersDAO().edit(new TbUsers(userId,userName,userNickName,userCardId,userWorkId,userPass,userPhone,userEmail,userAddress,userBirthday,userInCompanyTime,userState,parentUserId,createUserId,isEnable,userMemo,isAdmin))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
            goList(request, response);
        }
        if ("list".equals(method))goList(request, response);
        if ("listById".equals(method)) {
            request.getSession(true).setAttribute("tbUsers",new TbUsersDAO().getTbUsersByUserId(Integer.parseInt(request.getParameter("userId"))));
            response.sendRedirect("UsersModule/TbUsersOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession().removeAttribute("tbUsers");
            response.sendRedirect("UsersModule/TbUsersOperate.jsp");
        }
        if("login".equals(method)){
        	TbUsers tbusers=new TbUsersDAO().checkLogin(new TbUsers(request.getParameter("username"), request.getParameter("userpass")));
        	List<TbModule> tbModuleListPar = new TbModuleDAO().getTbModuleByParentModuleIdOrderBy(0);
        	Hashtable<TbModule,List<TbModule>> ht = new Hashtable<TbModule,List<TbModule>>();
        	
        	if(tbModuleListPar!=null&&tbModuleListPar.size()>0){
        		for(TbModule module:tbModuleListPar){
        			List<TbModule> tbModuleListlst = new TbModuleDAO().getTbModuleByParentModuleId(module.getModuleId());
        			if(tbModuleListlst!=null&&tbModuleListlst.size()>0){
        				ht.put(module, tbModuleListlst);
        			}else{
        				ht.put(module, tbModuleListlst);
        			}
        		}
        	}
        	if(tbusers!=null){
        		request.getSession(true).setAttribute("Users", tbusers);
        		request.getSession(true).setAttribute("tbModuleListPar", tbModuleListPar);
        		request.getSession(true).setAttribute("ht",ht);
        		response.sendRedirect("TbModuleServlet?method=toindex");
        	}else{
        		response.sendRedirect("login.jsp?err=1");
        	}
        }
        if("loginout".equals(method)){
        	request.getSession(true).removeAttribute("Users");
        	response.sendRedirect("login.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession(true).setAttribute("tbUsersList",new TbUsersDAO().getTbUsersAll());
        response.sendRedirect("UsersModule/TbUsersList.jsp");
    }// end method doGet
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
